package com.nnnu.wsnackshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nnnu.wsnackshop.exception.ObjectException;
import com.nnnu.wsnackshop.mapper.BannersMapper;
import com.nnnu.wsnackshop.pojo.dto.BannerDTO;
import com.nnnu.wsnackshop.pojo.entity.Banners;
import com.nnnu.wsnackshop.pojo.vo.BannerVO;
import com.nnnu.wsnackshop.service.IBannersService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 轮播图Service实现类
 */
@Service
public class BannersServiceImpl extends ServiceImpl<BannersMapper, Banners> implements IBannersService {

    @Override
    public IPage<BannerVO> getBannerPage(Page<Banners> page, String title, Integer status, String linkType) {
        IPage<Banners> bannerPage = baseMapper.selectBannerPage(page, title, status, linkType);

        return bannerPage.convert(banner -> {
            BannerVO vo = new BannerVO();
            BeanUtils.copyProperties(banner, vo);
            return vo;
        });
    }

    @Override
    @Cacheable(value = "banners", key = "'enabled'")
    public List<BannerVO> getEnabledBanners() {
        LambdaQueryWrapper<Banners> wrapper = Wrappers.lambdaQuery(Banners.class)
                .eq(Banners::getStatus, 1)
                .orderByAsc(Banners::getSortOrder);
        return convertToVOList(baseMapper.selectList(wrapper));
    }

    @Override
    public List<BannerVO> getUserBanners() {
        return getEnabledBanners();
    }

    /**
     * 将实体列表转换为VO列表
     */
    private List<BannerVO> convertToVOList(List<Banners> banners) {
        return banners.stream().map(banner -> {
            BannerVO vo = new BannerVO();
            BeanUtils.copyProperties(banner, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public BannerVO getBannerById(Integer id) {
        Banners banner = baseMapper.selectById(id);
        if (banner == null) {
            throw new ObjectException("轮播图不存在");
        }
        BannerVO vo = new BannerVO();
        BeanUtils.copyProperties(banner, vo);
        return vo;
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void addBanner(BannerDTO bannerDTO) {
        Banners banner = new Banners();
        BeanUtils.copyProperties(bannerDTO, banner);
        baseMapper.insert(banner);
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void updateBanner(BannerDTO bannerDTO) {
        Banners banner = baseMapper.selectById(bannerDTO.getId());
        if (banner == null) {
            throw new ObjectException("轮播图不存在");
        }
        BeanUtils.copyProperties(bannerDTO, banner);
        baseMapper.updateById(banner);
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void deleteBanner(Integer id) {
        if (baseMapper.deleteById(id) <= 0) {
            throw new ObjectException("轮播图不存在");
        }
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void deleteBanners(List<Integer> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void updateBannerStatus(Integer id, Integer status) {
        Banners banner = baseMapper.selectById(id);
        if (banner == null) {
            throw new ObjectException("轮播图不存在");
        }
        banner.setStatus(status);
        baseMapper.updateById(banner);
    }

    @Override
    @Transactional
    @CacheEvict(value = "banners", key = "'enabled'")
    public void updateBannerSort(Integer id, Integer sortOrder) {
        baseMapper.updateBannerSort(id, sortOrder);
    }
}